package com.ruoyi.web.controller.car;


import com.ruoyi.car.business.domain.CarPackageAudit;
import com.ruoyi.car.business.query.CarPackageAuditQuery;
import com.ruoyi.car.business.service.ICarPackageAuditService;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TablePageInfo;
import com.ruoyi.common.utils.ShiroUtils;
import org.apache.poi.util.IOUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;

/*
 * 岗位控制器
*/

@Controller
@RequestMapping("business/carPackageAudit")
public class CarPackageAuditController {
    //模板前缀
    private static final String prefix = "business/carPackageAudit/";

    @Autowired
    private ICarPackageAuditService carPackageAuditService;

    //页面------------------------------------------------------------
    //列表

    // 跳转到列表页面
    @RequiresPermissions("business:carPackageAudit:view")
    @RequestMapping("/listPage")
    public String listPage(){
        return prefix + "list";
    }

    // 跳转新增页面
    @RequiresPermissions("business:carPackageAudit:add")
    @RequestMapping("/addPage")
    public String addPage(){
        return prefix + "add";
    }

    // 跳转修改页面
    @RequiresPermissions("business:carPackageAudit:edit")
    @RequestMapping("/editPage")
    public String editPage(Long id, Model model){
        model.addAttribute("carPackageAudit", carPackageAuditService.get(id));
        return prefix + "edit";
    }

    // 跳转我的待办页面
    @RequestMapping("/todoPage")
    public String todoPage(){
        return prefix + "todoPage";
    }

    // 跳转到审批页面
    @RequestMapping("/auditPage")
    public String auditPage(Long id, Model model){
        model.addAttribute("id", id);
        return prefix + "audit";
    }

    // 跳转到我的已办页面
    @RequestMapping("/donePage")
    public String donePage(){
        return prefix + "donePage";
    }

    //数据-----------------------------------------------------------
    //列表
    @RequiresPermissions("business:carPackageAudit:list")
    @RequestMapping("/query")
    @ResponseBody
    public TablePageInfo<CarPackageAudit> query(CarPackageAuditQuery qo){
        qo.setUserId(ShiroUtils.getUserId());
        return carPackageAuditService.query(qo);
    }

    //新增
    @RequiresPermissions("business:carPackageAudit:add")
    @RequestMapping("/add")
    @ResponseBody
    public AjaxResult addSave(CarPackageAudit carPackageAudit){
        carPackageAuditService.save(carPackageAudit);
        return AjaxResult.success();
    }

    //编辑
    @RequiresPermissions("business:carPackageAudit:edit")
    @RequestMapping("/edit")
    @ResponseBody
    public AjaxResult edit(CarPackageAudit carPackageAudit){
        carPackageAuditService.update(carPackageAudit);
        return AjaxResult.success();
    }

    //删除
    @RequiresPermissions("business:carPackageAudit:remove")
    @RequestMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids){
        carPackageAuditService.deleteBatch(ids);
        return AjaxResult.success();
    }

    // 查看流程图
    @RequestMapping("/processImg")
    @ResponseBody
    public void processImg(Long id, HttpServletResponse response) throws IOException {
        InputStream inputStream = carPackageAuditService.getProcessImg(id);
        IOUtils.copy(inputStream,response.getOutputStream());
    }

    // 撤销申请
    @RequestMapping("/cancelApply")
    @ResponseBody
    public AjaxResult cancelApply(Long id){
        carPackageAuditService.cancelApply(id);
        return AjaxResult.success();
    }

    // 我的待办
    @RequestMapping("/todoQuery")
    @ResponseBody
    public TablePageInfo<CarPackageAudit> todoQuery(CarPackageAuditQuery qo){
        // 获取当前登陆用户
        SysUser user = ShiroUtils.getSysUser();
        // 通过当前用户的ID设置为审核人ID
        qo.setAuditorId(user.getUserId());
        // 设置当前的状态为审核中
        qo.setStatus(CarPackageAudit.STATUS_IN_ROGRESS);
        return carPackageAuditService.query(qo);
    }

    // 请求完成审核
    @RequestMapping("/audit")
    @ResponseBody
    public AjaxResult audit(Long id, int auditStatus, String info){
        carPackageAuditService.audit(id, auditStatus, info);
        return AjaxResult.success();
    }

    // 我的已办
    @RequestMapping("/doneQuery")
    @ResponseBody
    public TablePageInfo<CarPackageAudit> doneQuery(CarPackageAuditQuery qo){
        SysUser user = ShiroUtils.getSysUser();
        qo.setInfo(user.getUserName());
        return carPackageAuditService.query(qo);
    }
}
